﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using AmazonGlacierGUI.Core;
using AmazonGlacierGUI.Core.Helpers.AmazonGlacier;
using AmazonGlacierGUI.Core.Infrastructure;
using AmazonGlacierGUI.UI.Infrastructure;
using AmazonGlacierGUI.UI.Infrastructure.MVVMFramework;
using GalaSoft.MvvmLight.Command;

//08-Sep-2012 Priyan R

namespace AmazonGlacierGUI.UI.ViewModels
{
    public class VaultBrowserViewModel : AppViewModelBase
    {
        public VaultBrowserViewModel(AmazonRegionEnum region)
        {
            _region = region;
            WireCommands();
        }

        private void OnOkCommand()
        {
            Close();
        }

        public void OnRegionChanged()
        {
            LoadVaults(false);
        }

        private void OnReloadCommand()
        {
            LoadVaults(true);
        }

        public void Init()
        {
            LoadVaults(false);
        }

        private void LoadVaults(bool reLoad)
        {
            IsBusy = true;
            System.Threading.Tasks.Task<List<VaultDetails>>.Factory.StartNew(
                () => { return AppContext.Current.GetServiceFactory().AppService.GetVaultDetailsByRegion(Region, reLoad); })
                .ContinueWith((t) =>
                {
// ReSharper disable ConvertToLambdaExpression
                    ExecuteOnUI(() =>
                    {
                        if (t.Exception != null)
                        {
                            UIHelper.ShowError("Error occured " + AppUtility.GetExceptionMessage(t.Exception) , () => { });
                        }
                        else
                        {
                            Vaults = t.Result;
                        }
                        IsBusy = false;
                    });
// ReSharper restore ConvertToLambdaExpression
                });
        }

        private void WireCommands()
        {
            ReloadCommand = new RelayCommand(OnReloadCommand, () => !IsBusy);
            OkCommand = new RelayCommand(OnOkCommand, () => SelectedVault != null);
        }

        public override string OnClose()
        {
            SelectedVault = null;
            return null;
        }
        protected override void OnCloseCommand()
        {
            OnClose();
            Close();
        }
        private VaultDetails _selectedVault;
        public VaultDetails SelectedVault
        {
            get { return _selectedVault; }
            set
            {
                if (_selectedVault != value)
                {
                    _selectedVault = value;                    
                    RaisePropertyChanged(() => SelectedVault);
                }
            }
        }
        private List<VaultDetails> _vaults;
        public List<VaultDetails> Vaults
        {
            get { return _vaults; }
            set
            {
                if (_vaults != value)
                {
                    _vaults = value;
                    RaisePropertyChanged(() => Vaults);
                }
            }
        }

        private bool _canReload;

        public bool CanReload
        {
            get { return _canReload; }
            set
            {
                if (_canReload != value)
                {
                    _canReload = value;
                    RaisePropertyChanged(() => CanReload);
                }
            }
        }

        private string _reloadMessage;

        public string ReloadMessage
        {
            get { return _reloadMessage; }
            set
            {
                if (_reloadMessage != value)
                {
                    _reloadMessage = value;
                    RaisePropertyChanged(() => ReloadMessage);
                }
            }
        }
           
        public RelayCommand ReloadCommand { get; set; }
        public RelayCommand OkCommand { get; set; }
        public RelayCommand CancelCommand { get; set; }

        private AmazonRegionEnum _region;

        [Required]
        public AmazonRegionEnum Region
        {
            get { return _region; }
            set
            {
                if (_region != value)
                {
                    _region = value;
                    RaisePropertyChanged(() => Region);
                    OnRegionChanged();
                }
            }
        }

        private bool _isBusy;

        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                if (_isBusy != value)
                {
                    _isBusy = value;
                    RaisePropertyChanged(() => IsBusy);
                    ReloadCommand.RaiseCanExecuteChanged();
                }
            }
        }
    }
}